# -*- encoding: utf-8 -*-
"""
@File        : dc_manager.py
@License     : (C)Copyright 2021 LeeRoc (cn) Lipeng
@Modify Time : 2021/8/1 10:34 上午
@Author      : LeeRoc
@Version     : 1.0
@Description : 实现对host集群的状态管理，以及分发管理
通过调用host_dc_manager，初始化hosts的数据状态，
本类设计了若干个rpc服务函数，旨在通过在这里调用底层rpc函数，实现远端控制集群，
然后在次封装，让更上一层的app调用。

本类也实现了更新远端remote的函数，用来检测远端状态，并通过sqlite进行管理，
若其他app想要调用，即可先通过更新数据库远端机状态，然后访问数据库即可

"""
import sys
from time import sleep

sys.path.append('../')

import xmlrpc.client

from config.Config import *
from core.os_status_unit import get_localhost_ip
from core.heartbeat_detection_unit import *
from core.data_access_object import host_table

hosts = Get_Cluster_Ip_By_Conf().Get_host()
socketport = Get_globleconfig().Get_setting('socket', 'port')


class host_dc_manager:
    def __init__(self):
        self.host_manager = host_table()
        for host in hosts:
            self.host_manager.insert_host(host[0], '22', socketport,
                                          socket_port_check(host[0], 22), socket_port_check(host[0], int(socketport))
                                          , delay_check(host[0]), '0', '0')  # 初始化CPU&memory为0

    def __rpc_init(self, hostip: list, port: str):
        rpc_manager = xmlrpc.client.ServerProxy('http://%s:%s' % (hostip, port))
        return rpc_manager

    def __rpc_serverfunction_cpustatus(self, rpc_server):
        return rpc_server.cpu_status()

    def __rpc_serverfunction_memory(self, rpc_server):
        return rpc_server.internalmemory_status()

    def __rpc_serverfunction_distribute(self, rpc_server, host: list, localhost_file: str, cluster_file: str):
        rpc_server.distribute(host, localhost_file, cluster_file)

    def __rpc_serverfunction_collect(self, rpc_server, host: list, cluster_file: str, localhost_file: str):
        rpc_server.collect(host, cluster_file, localhost_file)

    def disaster_recovery_strategy(self):
        pass

    def remote_status_update(self):
        """
        更新远端机状态在数据库中
        """
        for host in hosts:
            if host[0] != get_localhost_ip():
                self.host_manager.update_host_portstatus(host[0], socket_port_check(host[0], 22), 1)
                self.host_manager.update_host_portstatus(host[0], socket_port_check(host[0], int(socketport)), 2)
                self.host_manager.update_host_netstatus(host[0], delay_check(host[0]))
                self.host_manager.update_host_internalmem(host[0], self.__rpc_serverfunction_memory(
                    self.__rpc_init(host[0], socketport)))
                self.host_manager.update_host_cpustatus(host[0], self.__rpc_serverfunction_cpustatus(
                    self.__rpc_init(host[0], socketport)))
                print(self.host_manager.select_host_ipall(host[0]))

    def call_rpc_distribute(self, hostip: list, localhost_file: str, cluster_file: str):
        """
        调用远端的分发函数，请求master分发，可用线程分发，提高分发效率
        :param hostip: 从配置文件中读取的IP信息
        :param localhost_file: 本地发送的文件
        :param cluster_file: 发送到远端文件的什么位置
        """
        # 如果是本机IP，则不发送
        if hostip[0] != get_localhost_ip():
            self.__rpc_serverfunction_distribute(self.__rpc_init(hostip[0], socketport), hostip, localhost_file,
                                                 cluster_file)

    def call_rpc_colletc(self, hostip: list, cluster_file: str, local_file: str):
        """
        调用收集函数，即本机向远端请求，一般为队列申请
        :param hostip:
        :param cluster_file:
        :param local_file:
        """
        # 如果是本机IP，则不收集
        if hostip[0] != get_localhost_ip():
            self.__rpc_serverfunction_collect(self.__rpc_init(hostip[0], socketport), hostip, cluster_file, local_file)

    def close(self):
        self.host_manager.close()
